package com.imooc.business;


import com.imooc.business.processor.processor.QueryDateProcessor;
import com.imooc.business.util.report.DateUtil;
import com.imooc.business.util.report.ReportUtil;
import com.imooc.module.reponse.QueryDateMetaResponse;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Repository
public class DateMetaBusiness {

    @Resource(name = "queryDayMetaDataProcessor")
    private QueryDateProcessor queryDateProcessor;

    /**
     * 时间维度元数据查询
     * @return
     */
    public QueryDateMetaResponse queryDateMetaData() {
        Date minTime = new Date(0L);
        Date now = new Date();

        QueryDateMetaResponse response = new QueryDateMetaResponse();
        QueryDateMetaResponse.Table table = new QueryDateMetaResponse.Table();
        response.setTable(table);
        List<QueryDateMetaResponse.DateView> dateViews = new ArrayList<>();
        table.setItems(dateViews);

        queryDateProcessor.buildDateViews(ReportUtil.generateReportDayItems(minTime, DateUtil.getBeforeDate(now, 1), 1, true), dateViews);
        queryDateProcessor.buildDateViews(ReportUtil.generateReportWeekItems(minTime, now, 1, true), dateViews);
        queryDateProcessor.buildDateViews(ReportUtil.generateReportMonthItems(minTime, now, 1, true), dateViews);
        queryDateProcessor.buildDateViews(ReportUtil.generateReportTermItems(minTime, now, 1, true), dateViews);
        queryDateProcessor.buildDateViews(ReportUtil.generateReportYearItems(minTime, now, 1, true), dateViews);

        return response;
    }


    /**
     * 时间维度周期数据查询
     * @param periodType
     * @return
     */
    public QueryDateMetaResponse queryDatePeriodData(Integer periodType) {
        QueryDateMetaResponse response = new QueryDateMetaResponse();
        QueryDateMetaResponse.Table table = new QueryDateMetaResponse.Table();
        response.setTable(table);
        List<QueryDateMetaResponse.DateView> dateViews = new ArrayList<>();
        table.setItems(dateViews);

        queryDateProcessor.queryProcessor(dateViews, periodType);
        return response;
    }

}
